#!bin/bash

if [ -n "$1" ] ; then
  data_date=$1
else
  data_date=`date -d '-1 day' +%F`
fi


DWD_CUSTOMER_ORDER_DISCOUNT_SQL="
INSERT OVERWRITE TABLE jtp_ds_warehouse.dwd_customer_order_discount PARTITION (dt = '${data_date}')
SELECT
    o.order_id,
    o.user_id,
    o.shop_id,
    o.order_time,
    o.pay_time,
    o.total_amount,
    o.pay_amount AS order_pay_amount,
    o.cs_discount_amount,
    o.other_discount_amount,
    o.order_status,
    dv.id AS verify_id,
    dv.send_id,
    dv.pay_amount AS verify_pay_amount,
    dv.actual_discount,
    dv.verify_time,
    ds.activity_id,
    ds.product_id,
    ds.sku_id,
    ds.customer_service_id,
    ds.send_discount,
    ds.send_time,
    ds.valid_period,
    ds.expire_time,
    ds.is_used,
    cs.cs_id,
    cs.cs_name,
    cs.shop_id AS cs_shop_id,
    cs.status AS cs_status,
    cs.create_time AS cs_join_time,
    ca.activity_name,
    ca.activity_level,
    ca.discount_type,
    ca.min_discount,
    ca.max_discount,
    ca.start_time AS activity_start_time,
    ca.end_time AS activity_end_time,
    ca.status AS activity_status,
    ca.create_time AS activity_create_time,
    ca.create_user,
    shop.shop_name,
    shop.platform_type,
    shop.status AS shop_status
FROM jtp_ds_warehouse.ods_order_info o
         LEFT JOIN jtp_ds_warehouse.ods_discount_verification_record dv
                   ON o.order_id = dv.order_id
                       AND o.dt = dv.dt
         LEFT JOIN jtp_ds_warehouse.ods_discount_send_detail ds
                   ON dv.send_id = ds.id
                       AND dv.dt = ds.dt
         LEFT JOIN jtp_ds_warehouse.ods_customer_service_info cs
                   ON ds.customer_service_id = cs.cs_id
                       AND ds.dt = cs.dt
         LEFT JOIN jtp_ds_warehouse.ods_shop_info shop
                   ON o.shop_id = shop.shop_id
                       AND o.dt = shop.dt
         LEFT JOIN jtp_ds_warehouse.ods_customer_service_discount_activity ca
                   ON ds.activity_id = ca.activity_id
                       AND ds.dt = ca.dt
WHERE o.dt = '${data_date}'
  AND dv.dt = '${data_date}'
  AND ds.dt = '${data_date}'
  AND ca.dt = '${data_date}'
ORDER BY o.order_time DESC;
"

/opt/module/spark/bin/beeline -u jdbc:hive2://node101:10001 -n bwie -p 123456 -e "${DWD_CUSTOMER_ORDER_DISCOUNT_SQL}"
